package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.*;
import com.zb.mapper.ZjWorkClientStrategyEmpMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjWorkClientStrategyEmpService;
import com.zb.service.ZjWorkTeamEmpService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 工单客服分派策略人员表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-09-27
 */
@Service
public class ZjWorkClientStrategyEmpServiceImpl extends ServiceImpl<ZjWorkClientStrategyEmpMapper, ZjWorkClientStrategyEmp> implements ZjWorkClientStrategyEmpService {
    @Resource
    private ZjWorkClientStrategyEmpMapper workClientStrategyEmpMapper;
    @Resource
    private ZjWorkTeamEmpService workTeamEmpService;

    // 保存客服/客服组
    @Override
    public void save(Long custAccId, Long workClientStrategyId, List<Long> serviceIds) {
        workClientStrategyEmpMapper.deleteByWorkClientStrategyId(workClientStrategyId);
        if (null == serviceIds || serviceIds.isEmpty()) {
            return;
        }
        List<ZjWorkClientStrategyEmp> emps = new ArrayList<>();
        ZjWorkClientStrategyEmp emp = null;
        for (Long serviceId : serviceIds) {
            emp = new ZjWorkClientStrategyEmp();
            emp.setId(IdWorker.getId());
            emp.setCustAccId(custAccId);
            emp.setWorkClientStrategyId(workClientStrategyId);
            emp.setValId(serviceId);
            emps.add(emp);
        }
        if (!emps.isEmpty()) this.saveBatch(emps);
    }

    // 根据ID查询客服
    @Override
    public List<SelectEmpVo> getServiceEmpByWorkClientStrategyIds(List<Long> workClientStrategyIds) {
        if (null == workClientStrategyIds || workClientStrategyIds.isEmpty()) {
            return new ArrayList<>();
        }
        return workClientStrategyEmpMapper.getServiceEmpByWorkClientStrategyIds(workClientStrategyIds);
    }

    // 根据ID查询客服组
    @Override
    public List<SelectEmpVo> getServiceGroupByWorkClientStrategyIds(List<Long> workClientStrategyIds) {
        if (null == workClientStrategyIds || workClientStrategyIds.isEmpty()) {
            return new ArrayList<>();
        }
        return workClientStrategyEmpMapper.getServiceGroupByWorkClientStrategyIds(workClientStrategyIds);
    }

    // 根据ID获取人员类型关联ID
    @Override
    public List<ZjWorkClientStrategyEmp> getValIdByWorkClientStrategyId(Long workClientStrategyId) {
        QueryWrapper<ZjWorkClientStrategyEmp> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_client_strategy_id", "val_id");
        wrapper.eq("work_client_strategy_id", workClientStrategyId);
        return this.list(wrapper);
    }

    // 根据ID & 人员类型获取客服ID
    @Override
    public List<Long> getEmpIdByEmpTypeAndWorkClientStrategyId(Integer empType, Long workClientStrategyId) {
        List<Long> empIds = new ArrayList<>();
        QueryWrapper<ZjWorkClientStrategyEmp> wrapper = new QueryWrapper<>();
        wrapper.select("id", "val_id");
        wrapper.eq("work_client_strategy_id", workClientStrategyId);
        List<ZjWorkClientStrategyEmp> strategyEmpList = this.list(wrapper);
        if (1 == empType) {
            empIds = strategyEmpList.stream().map(ZjWorkClientStrategyEmp::getValId).toList();
        }
        if (2 == empType) {
            Long workTeamId = strategyEmpList.stream().map(ZjWorkClientStrategyEmp::getValId).findFirst().orElse(null);
            if (null != workTeamId) {
                empIds = workTeamEmpService.getByWorkTeamId(workTeamId).stream().map(ZjWorkTeamEmp::getEmpId).toList();
            }
        }
        return empIds;
    }

    // 根据客服组ID获取关联的客服组ID
    @Override
    public List<Long> getWorkTeamIdByWorkTeamIds(Long custAccId, List<Long> workTeamIds) {
        return workClientStrategyEmpMapper.getWorkTeamIdByWorkTeamIds(custAccId, workTeamIds);
    }
}
